// 后台管理总组件
<template>
    <el-container class="container">
        <!-- 头部 -->
        <el-header>
            <div class="header-left">
                <img src="../assets/img/cai_1.jpg" alt="">
                <span>猫哥菜菜网上书店后台管理系统</span>
            </div>
            <el-button type="info" @click="mangerlogout">退出登入</el-button>
        </el-header>

        <el-container>
          <!-- 侧边栏  根据是否折叠菜单动态决定宽度-->
          <el-aside :width="isCollasp ? '64px' : '200px'">
            <!-- 菜单是否折叠 -->
            <div class="toggle-button" @click="toggleCollapse">|||</div>  
            <!-- unique-opened是否每次只显示一个子菜单 
            collapse菜单是否水平折叠 
            collapse-transition水平折叠动画
            router开启路由跳转 path对应的是index的值
            default-active当前激活的菜单的index值-->
            <el-menu background-color="#2e4b6d" text-color="#fff" active-text-color="#409eff" 
                    :unique-opened="true" :collapse="isCollasp"
                    :collapse-transition="false" :router="true"
                    :default-active="activePath">
              <!-- 一级菜单 -->
              <el-submenu class="onegrade" :index="item.id+''" v-for="item in menuList" :key="item.id">
                <!-- 模板区域 -->
                <template slot="title">
                  <!-- 图标 文本 -->
                  <i class="icolor" :class="iconobj[item.id]"></i>
                  <span>{{item.authName}}</span>
                </template>
                <!-- 二级菜单 -->
                <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveActivePath('/'+subitem.path)">
                  <template slot="title">
                    <i class="icolor el-icon-menu"></i>
                    <span>{{subitem.authName}}</span>
                  </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
            <!-- 主体内容 -->
          <el-main>
            <router-view></router-view>
          </el-main>
        </el-container>   
    </el-container>
    
</template>

<script>
export default {
  data() {
    return {
      menuList:[],    //保存获取到的后端商品菜单列表
      iconobj:{    //修改一级菜单图标
        125:'iconfont icon-users',
        103:'iconfont icon-tijikongjian',
        101:'iconfont icon-shangpin',
        102:'iconfont icon-danju',
        145:'iconfont icon-baobiao',
      },
      isCollasp:false,    //是否折叠菜单
      activePath:'',    //当前激活菜单的index值
    }
  },
  created(){   
    this.getMenuList()  //在实例创建前调用获取后台商品的菜单数据
    this.activePath = window.sessionStorage.getItem('avtivePath')
  },
  methods: {
    mangerlogout() {   //退出登入
      window.sessionStorage.clear() //清除浏览器本地缓存 token值
      this.$router.push('/lorR/login') //编程式路由导航跳转到登入界面
    },
    async getMenuList(){  //获取后端的所有de商品菜单列表
      const {data:res} = await this.$http.get('menus')
      // console.log(res)
      if(res.meta.status !== 200 ) return this.$message.error(res.meta.msg)  //请求失败 弹窗显示失败内容
      this.menuList = res.data   //保存数据
      // console.log(res,this.menuList)
    },
    toggleCollapse(){   //点击是否折叠菜单
      this.isCollasp = !this.isCollasp
    },
    saveActivePath(avtivePath){   //保存当前激活菜单的index值到本地浏览器
      window.sessionStorage.setItem('avtivePath',avtivePath)
      this.activePath = avtivePath
    }
  },
}
</script>

<style lang="less" scoped>
.container{
    height: 100%;
}
.el-header{
    background-color: #2e4b6d;
    display: flex;
    padding-left: 0;
    align-items: center;
    .header-left{
        width: 100%;
        display: flex;
        color: #fff;
        font-size: 20px;
        align-items: center;   //纵向对齐
        img{
            height: 50px;
            margin: 10px;
            margin-right: 15px;
            border-radius: 50%;
            box-shadow: 0 0 10px #fff;
        }
        span{
            letter-spacing: 3px;
        }
    }
}
.el-aside{
  background-color: #2e4b6d ;
  .el-menu{
    border-right: none;
  }
}
.el-main{
    background-color: #eaedf1;
}
.icolor{
  margin-right: 20px;
  color: #fff;
}
.toggle-button{
  background-color: rgb(126, 149, 156);
  font-size: 15px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.15em;
  cursor: pointer;
  color: #fff;
  font-weight: bold;
}
</style>